﻿CREATE PROCEDURE [acms].[UserSecurity_Put]
	@Id int, 
	@SiteId int,
	@FirstName varchar(50),
	@LastName varchar(50),
	@UserName varchar(50),
	@DateOfBirth DateTime,
	@EmailAddress varchar(100),
	@Gender tinyint,
	@Address1 varchar(100),
	@Address2 varchar(100),
	@Town varchar(50),
	@PostCode varchar(20),
	@MobileTel varchar(20),
	@Created DateTime,
	@Password varchar(255),
	@Salt int,
	@ConcurencyId RowVersion,
	@IdOut int output,
	@ConcurencyIdOut RowVersion output
AS
	if(@SiteId = 0)
		set @SiteId = null
	if exists (select null from User_View where Id = @Id) begin
		update Users
		Set
			SiteId = @SiteId,
			FirstName = @FirstName,
			LastName = @LastName,
			UserName = @UserName,
			DateOfBirth = @DateOfBirth,
			EmailAddress = @EmailAddress,
			Gender = @Gender,
			Address1 = @Address1,
			Address2 = @Address2,
			Town = @Town,
			PostCode = @PostCode,
			MobileTel = @MobileTel,
			Password = @Password,
			Salt = @Salt
		Where Id = @Id and VersionId = @ConcurencyId
		if @@RowCount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else begin
		insert into Users
			(SiteId, FirstName, LastName, UserName, DateOfBirth, EmailAddress, Gender, Address1, Address2, Town, PostCode, MobileTel, Created, Password, Salt)
		values
			(@SiteId, @FirstName, @LastName, @UserName, @DateOfBirth, @EmailAddress, @Gender, @Address1, @Address2, @Town, @PostCode, @MobileTel, @Created, @Password, @Salt)
		set @IdOut = @@Identity
	end
	
	select @ConcurencyIdOut = VersionId From User_View where Id = @IdOut
	
RETURN 0;